﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
using System.Windows.Data;

namespace LotteryAdmin
{
    public partial class LogFgView : UserControl
    {
        LotteryAdminClient srv;
        public LogFgView()
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            srv.GetFgLogDataCompleted+=new EventHandler<GetFgLogDataCompletedEventArgs>(srv_GetFgLogDataCompleted);
            dtFrom.Text = DateTime.Now.ToShortDateString();
            dtTo.Text = DateTime.Now.ToShortDateString();
            this.KeyDown += new KeyEventHandler(Page_KeyDown);
        }
        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == 13)
                this.btnSearch_Click(this.btnSearch, null);
        }

        private void srv_GetFgLogDataCompleted(object sender, GetFgLogDataCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            PagedCollectionView pageView = new PagedCollectionView(e.Result);
            dpList.Source = pageView;
            dgList.ItemsSource = pageView;
            if (e.Result.Count == 0)
            {
                MessageBox.Show("没有找到匹配的记录!");
            }
        }

        private void dgList_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            (this.dgList.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (dpList.PageIndex * dpList.PageSize + e.Row.GetIndex() + 1).ToString();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate.ValidateName(txtUserName.Text.Trim()))
            {
                MessageBox.Show("请输入正确的用户名！");
                return;
            }
            if (string.IsNullOrEmpty(dtFrom.Text) || string.IsNullOrEmpty(dtTo.Text))
            {
                MessageBox.Show("请选择正确的时间！");
                return;
            }
            if (dtTo.SelectedDate.Value < dtFrom.SelectedDate.Value)
            {
                MessageBox.Show("开始时间不能大于结束时间！");
                return;
            }
            srv.GetFgLogDataAsync(txtUserName.Text.Trim(), dtFrom.SelectedDate.Value, dtTo.SelectedDate.Value.AddDays(1));
            loadingProgress.IsBusy = true;
        }
    }
}
